package com.neusoft.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.mapper.EmpSqlMapper;
import com.neusoft.model.EmpModel;
import com.neusoft.model.entity.Emp;
import com.neusoft.model.entity.EmpExample;
import com.neusoft.service.EmpService;

//把当前类注册给Spring容器,作为service组件
@Service 
public class EmpServiceImpl implements EmpService{
	
	//如果调用自定义的sql, 则用我们自定义的Mapper接口声明
	
	//使用spring框架自动注入进来. byType
	@Autowired
	EmpSqlMapper empMapper;
	
	@Override
	public Emp loadEmpByEmpno(Integer empno) {
		return empMapper.selectByPrimaryKey(empno);
	}

	@Override
	public EmpModel loadEmpModelByEmpno(Integer empno) {
		return empMapper.loadEmpModelByEmpno(empno);
	}

	@Override
	public List<Emp> loadEmpList() {
		//查询所有员工实体, 返回列表
		return empMapper.selectByExample(null);
	}

	@Override
	public List<Emp> loadEmpListByDeptno(Integer deptno) {
		EmpExample e = new EmpExample();
		//创建规则
		EmpExample.Criteria c = e.createCriteria();
		//设置查询规则
		c.andDeptnoEqualTo(deptno);
		
		//根据规则查询列表
		return empMapper.selectByExample(e);
	}

	@Override
	public List<Emp> loadEmpListByJob(String job) {
		EmpExample e = new EmpExample();
		//创建规则
		EmpExample.Criteria c = e.createCriteria();
		//设置查询规则
		c.andJobEqualTo(job);
		
		//根据规则查询列表
		return empMapper.selectByExample(e);
	}

	@Override
	@Transactional //加入事务管理器, 如果出异常,自动回滚
	public int removeEmp(Integer empno) {
		int rowCount = empMapper.deleteByPrimaryKey(empno);
		Object obj = null; obj.toString();
		return rowCount;
	}

}
